
<?php

$nomRep = 'test_compi';


global $nomRep, $resultat, $nbProgramme, $nbProgramme, $nbProgramme_succes, $nbProgramme_fail;

$resultat = "\n\n\t Nom Programme \t | \t ETAT TYPE \t | \t ETAT COMPI \t | \t ETAT MINIC \n";
$resultat .="-------------------------------------------------------------------------------------------------------- \n"; 

$nbProgramme = 0;

$nbProgramme_succes = 0;
$nbProgramme_fail = 0;

$file = "addresse1.c";

if(isset($argv[1])){
	$i = 1;
	while(isset($argv[$i])){
		checkFile($argv[$i]);
		$i++;
	}
}else{

	$rep = opendir($nomRep); 
	while($file = readdir($rep)) {
			checkFile($file);
	}

}

closedir($rep);

$resultat.="-----------------\n\n";
$resultat.="Nombre de programme Teste \t:\t $nbProgramme\n";
$resultat.="Nombre de programme Succes \t:\t $nbProgramme_succes sur $nbProgramme\n";
$resultat.="Nombre de programme Fail \t:\t $nbProgramme_fail sur $nbProgramme\n";
$resultat.="\nNote \t\t\t\t:\t ".(($nbProgramme_succes/$nbProgramme)*100)."%\n";

echo $resultat;


function checkFile($file){

	global $nomRep, $resultat, $nbProgramme, $nbProgramme, $nbProgramme_succes, $nbProgramme_fail;
	
	$op = array();
	//On veut des fichier C uniquement
	if($file != '.' && $file != '..' && !is_dir($nomRep."/".$file)){
		if(preg_match('/\.c$/i', $file)){		
			echo "## TESTING : $file  \n";
			
			$nbProgramme++;
		
			if(strlen($file)<6)
				$resultat.="| ". $file . "\t\t\t : ";
			else if(strlen($file)<14)
				$resultat.="| ". $file . "\t\t : ";
			else if(strlen($file)<21)
				$resultat.="| ". $file . "\t : ";
			else
				$resultat.="| ". $file . " : ";			
			
			$res1 = false;
			$exec = exec("../src-old/minic ".$nomRep."/".$file ,$op, $code);
			if($code == 2){
				$resultat.="\t !!ANOMALY!!\t | \tFAIL \t\t | \t !! FAIL !! \t | \t";
				$nbProgramme_fail++;
			}else if($code == 0){
				echo "# Compilation Succes : $file  , Num $nbProgramme\n";
				$res1 = true;
				$resultat.="\tSUCCES \t\t | \t";
				
				$file2 = str_replace('.c', '', $file);
				
				exec("java -jar Mars_4_2.jar ".$nomRep."/".$file2.".s >".$nomRep."/".$file2.".out2 2>&1  & echo $!" ,$op, $code);				
				$pid = (int)$op[sizeof($op)-1];
				

				sleep(5);
				$s_time = time();
				while(PsExists($pid) && $s_time + 60 > time())
					sleep(1);
					
				if(PsExists($pid)) {
					PsKill($pid);
					echo "# Mars Killed  \n";
				}
				
				$op = explode("\n", file_get_contents ($nomRep.'/'.$file2.'.out2'));
				$i = 0;
				while($i<sizeof($op) && strcmp($op[$i], "MARS 4.2  Copyright 2003-2012 Pete Sanderson and Kenneth Vollmar")!=0){
					$i++;
				}
				
				$i = 0;
				$out_file = "";
				$out = explode("\n", trim(file_get_contents ($nomRep.'/'.$file2.'.out')));
				
				if(sizeof($op) > (sizeof($out)+50)){
					$text = "";
				}else{$i = $i+2;
					$text = "";
					while($i<sizeof($op)){
						$text .= $op[$i];
						$i++;
					}
				}
				$text = trim($text);
				
				$i = 0;
				while($i<sizeof($out)){
					$out_file .= $out[$i];
					$i++;
				}
				
				echo "# Excpecting : \n";
				echo '"'.$out_file.'"';
				echo "\n# Got : \n";
				echo '"'.$text.'"'."\n";
				echo "#############################################\n\n";
				
				if(strcmp($out_file, $text)==0){
					$resultat.="SUCCES \t\t | \t SUCESS \t |";
					$nbProgramme_succes++;
				}else{
					$resultat.="FAIL  \t\t | \t !! FAIL !! \t |";
					$nbProgramme_fail++;
				}
			
			
			
			}else if($code != 0){
				$resultat.="\t    FAIL    \t | \tFAIL \t\t | \t !! FAIL !!\t | \t";
				$nbProgramme_fail++;
			}
			
			
			$resultat.="\n";
					
		}
	}
}

function PsKill($pid) { 
	exec("kill -9 $pid", $output); 
}  
				
function PsExists($pid) { 

    exec("ps | grep $pid 2>&1", $output); 

    while( list(,$row) = each($output) ) { 
			
            $row_array = explode(" ", $row); 
            $check_pid = $row_array[0]; 
            if($pid == $check_pid || $pid == $row) { 
                    return true; 
            } 

    } 

    return false; 
} 

